using MesDashboardApi.Api.Hubs;
using MesDashboardApi.Api.Services;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

// 添加 SignalR
builder.Services.AddSignalR();

// 添加跨域支持
builder.Services.AddCors(options =>
{
    options.AddPolicy("AllowAll", builder =>
    {
        builder.WithOrigins("http://localhost:5173")
               .AllowAnyMethod()
               .AllowAnyHeader()
               .AllowCredentials();
    });
});

// 注册服务
builder.Services.AddSingleton<DashboardDataService>();
builder.Services.AddHostedService<DashboardBackgroundService>();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

// 启用跨域 - 确保在其他中间件之前调用
app.UseCors("AllowAll");

app.UseHttpsRedirection();

app.UseAuthorization();

// 映射控制器
app.MapControllers();

// 映射 SignalR Hub
app.MapHub<DashboardHub>("/dashboardHub");

app.Run();
